import java.util.HashMap;

class Solution {
    public int totalFruit(int[] fruits) {
        int sum=0;
        int left=0;
        int right=0;
        int count=0;
        HashMap<Integer,Integer> hash=new HashMap<>();

        while(right<fruits.length){
            int key=fruits[right];
            if(hash.getOrDefault(key,0)==0){
                count++;
            }
            hash.put(key,hash.getOrDefault(key,0)+1);


            while(left<=right&&count>2){
                sum=Math.max(sum,right-left);
                System.out.println(right);
                System.out.println(left);

                key=fruits[left++];

                hash.put(key,hash.get(key)-1);
                if(hash.get(key)==0){
                    count--;
                }
            }
            right++;
        }
        sum=Math.max(sum,right-left);
        return sum;
    }
}